package com.metadata_system.system.service;


import com.metadata_system.common.db.entity.ProjectUser;
import com.metadata_system.common.db.mapper.ProjectUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProjectUserService {
    @Autowired(required = false)
    ProjectUserMapper projectUserMapper;
    @Autowired
    MessageService messageService;


    /**
     * 变动项目用户
     *
     * @param projectId
     * @param projectName
     * @param projectUsers
     */
    public void addProjecrUser(Integer projectId, String projectName, List<ProjectUser> projectUsers) {
        if (projectUsers.isEmpty()) {
            return;
        }

        // 变动后移出用户
        Example example = new Example(ProjectUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("projectId", projectId);
        List<Integer> updateUserIds = projectUsers.stream().map(ProjectUser::getUserId).collect(Collectors.toList());

        List<ProjectUser> userList = this.projectUserMapper.selectByExample(example);
        List<Integer> addedUserIds = userList.stream().map(ProjectUser::getUserId).collect(Collectors.toList());


        Map<Integer, ProjectUser> updateUserMap = new HashMap<>();
        projectUsers.forEach(user -> updateUserMap.put(user.getUserId(), user));

        // 变动后移入用户
        for (Integer addedUserId : addedUserIds) {
            if (!updateUserIds.contains(addedUserId)) {
                // 如果用户被添加
                messageService.addMessage(
                        "您已被添加到项目【" + projectName + "】中，角色为【" + updateUserMap.get(addedUserId).getRole() + "】",
                        "ProjectUser",
                        addedUserId,
                        projectId,
                        projectName,
                        updateUserMap.get(addedUserId).getId().toString());
            }
        }


        // 变动后移出用户,更新用户
        if (!userList.isEmpty()) {
            for (ProjectUser projectUser : userList) {
                if (!updateUserIds.contains(projectUser.getUserId())) {
                    // 如果用户被移除
                    messageService.addMessage(
                            "您已经从项目【" + projectName + "】中移除用户【" + projectUser.getRole() + "】",
                            "ProjectUser",
                            projectUser.getUserId(),
                            projectId,
                            projectName,
                            projectUser.getId().toString());

                } else {
                    // 如果任然存在，且角色发生变动(更新角色)
                    if (!projectUser.getRole().equals(updateUserMap.get(projectUser.getUserId()).getRole())) {
                        messageService.addMessage(
                                "您被设置为项目【" + projectName + "】成员中，角色为【" + projectUser.getRole() + "】",
                                "ProjectUser",
                                projectUser.getUserId(),
                                projectId,
                                projectName,
                                projectUser.getId().toString());
                        this.projectUserMapper.updateByPrimaryKeySelective(ProjectUser.builder()
                                .id(projectUser.getId())
                                .role(updateUserMap.get(projectUser.getUserId()).getRole())
                                .build());
                    }
                }
            }

        }
    }
}
